package cn.loveq.android;

import java.util.List;

import weibo4android.Paging;
import weibo4android.Status;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.loveq.android.R;

public class OAuthActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timeline);
		Uri uri = this.getIntent().getData();
		try {
			RequestToken requestToken = OAuthConstant.getInstance().getRequestToken();
			AccessToken accessToken = requestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));
			OAuthConstant.getInstance().setAccessToken(accessToken);
			TextView textView = (TextView) findViewById(R.id.TextView01);
			textView.setText("得到AccessToken的key和Secret,可以使用这两个参数进行授权登录了.\n Access token:\n" + accessToken.getToken() + "\n Access token secret:\n" + accessToken.getTokenSecret());
			Log.i("test"," Access token:" + accessToken.getToken() + "\n Access token secret:\n" + accessToken.getTokenSecret());
			
			Weibo weibo = new Weibo();
			String Token = accessToken.getToken();
			String TokenSecret = accessToken.getTokenSecret();
			// 在这里拿到了 Token 与 TokenSecret 可以把它存在Shared Preferences 中以后每次独取出来就可以用
			weibo.updateStatus("将Token 与 TokenSecret 存入 Shared Preferences ");
		} catch (WeiboException e) {
			e.printStackTrace();
		}

		Button button = (Button) findViewById(R.id.Button01);
		button.setText("某一话题下的微博");
		button.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {
				Weibo weibo = OAuthConstant.getInstance().getWeibo();
				weibo.setToken(OAuthConstant.getInstance().getToken(), OAuthConstant.getInstance().getTokenSecret());
				List<Status> friendsTimeline;
				try {
					friendsTimeline = weibo.getTrendStatus("seaeast", new Paging(1, 20));
					StringBuilder stringBuilder = new StringBuilder("");
					for (Status status : friendsTimeline) {
						stringBuilder.append(status.getUser().getScreenName() + "说:\n" + status.getText() + "\n--------------------------------------------------\n");
					}
					TextView textView = (TextView) findViewById(R.id.TextView01);
					textView.setText(stringBuilder.toString());
				} catch (WeiboException e) {
					e.printStackTrace();
				}

			}
		});
		//		
	}
}
